package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;

public class QuickColorDrawable extends ColorDrawable implements QuickDrawable {
    float[] mRadii;
    float mRadius = 0;

    public QuickColorDrawable(ColorDrawable colorDrawable) {
        super();
        int color = colorDrawable.getColor();
        setColor(color);
        setBounds(colorDrawable.getBounds());
    }

    @Override
    public void setCornerRadii(float[] radii) {
        this.mRadii = radii;
    }

    public float[] getCornerRadii() {
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.P) {
            return getCornerRadii();
        } else {
            return null;
        }
    }

    @Override
    public void setCornerRadius(float radius) {
        mRadius = radius;
        invalidateSelf();
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;
    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        mStrokePaint = new Paint();
        mStrokeColors = borderColor;
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(borderWidth);
        mStrokePaint.setColor(borderColor.getColorForState(getState(), 0));
        mStrokeWidth = borderWidth;
        invalidateSelf();
    }

    public float getCornerRadius() {
        return mRadius;
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();
        Rect rect = getBounds();
        RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
        Path path1 = new Path();
        if(mRadii!=null) {
            path1.addRoundRect(rectF, mRadii, Path.Direction.CW);
        }else {
            path1.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
        }
        canvas.clipPath(path1);
        super.draw(canvas);
        canvas.restore();
        if(mStrokePaint!=null) {
            canvas.drawPath(path1, mStrokePaint);
        }
    }
}
